﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace rePose
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        Keys keydata = Keys.None;

        private void button1_Click(object sender, EventArgs e)
        {
            ShortcutForm sf = new ShortcutForm();
            sf.ShowDialog();
            if (sf.keydata != Keys.None)
            {
                keydata = sf.keydata;
                lblShortcut.Text = keydata.ToString();
            }

        }


        private void ConfigForm_Load(object sender, EventArgs e)
        {
            Config.Instance().Load();
            keydata = Config.Instance().shortcut;
            lblShortcut.Text = Config.Instance().shortcutText;
            chkShortcut.Checked = Config.Instance().shortcutEnabled;
            chkLB.Checked = Config.Instance().cornerLB;
            chkRB.Checked = Config.Instance().cornerRB;
            chkRT.Checked = Config.Instance().cornerRT;
            chkLT.Checked = Config.Instance().cornerLT;
            chkShowNumber.Checked = Config.Instance().showNumbers;
            chkShowTitles.Checked = Config.Instance().showTitles;
            chkShowProcess.Checked = Config.Instance().showNames;
        }

        private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Config.Instance().shortcut = keydata;
            Config.Instance().shortcutEnabled = chkShortcut.Checked;
            Config.Instance().cornerLB = chkLB.Checked;
            Config.Instance().cornerRB = chkRB.Checked;
            Config.Instance().cornerRT = chkRT.Checked;
            Config.Instance().cornerLT = chkLT.Checked;
            Config.Instance().showNumbers = chkShowNumber.Checked;
            Config.Instance().showTitles = chkShowTitles.Checked;
            Config.Instance().showNames = chkShowProcess.Checked;
            Config.Instance().Save();
        }
    }
    class Config
    {
        const string REGISTRY_KEY_SHORTCUT_ENABLED = "shortcutEnabled";
        const string REGISTRY_KEY_SHORTCUT = "shortcut";
        const string REGISTRY_KEY_CORNER_LT = "cornerLT";
        const string REGISTRY_KEY_CORNER_RT = "cornerRT";
        const string REGISTRY_KEY_CORNER_LB = "cornerLB";
        const string REGISTRY_KEY_CORNER_RB = "cornerRB";
        const string REGISTRY_KEY_SHOW_NUMBERS = "showNumbers";
        const string REGISTRY_KEY_SHOW_TITLES = "showTitles";
        const string REGISTRY_KEY_SHOW_NAME = "showNames";
        const string REGISTRY_KEY_PATH = "SOFTWARE\\rePose";

        public string shortcutText;
        public Keys shortcut;
        public bool shortcutEnabled, cornerLT, cornerRT, cornerRB, cornerLB, showTitles, showNumbers, showNames;
        private static Config instance = null;

        string readString(RegistryKey key, string name, string def)
        {
            object z = key.GetValue(name, def);
            return (string)z;
        }

        Keys readKeys(RegistryKey key, string name)
        {
            int tmp = 0;
            object z = key.GetValue(name, tmp);
            Keys k = new Keys();
            k = (Keys)z;
            return k;
        }


        bool readBool(RegistryKey key, string name, bool def)
        {
            bool b = false;
            object z = key.GetValue(name, def.ToString());
            Boolean.TryParse((string)z, out b);
            return b;
        }

        private Config()
        {
            Load();
        }

        public void Load()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_KEY_PATH, true);
            if (key == null) key = Registry.CurrentUser.CreateSubKey(REGISTRY_KEY_PATH, RegistryKeyPermissionCheck.ReadWriteSubTree);
            shortcut = readKeys(key, REGISTRY_KEY_SHORTCUT);
            if (shortcut == Keys.None)
                shortcutText = "[ Not Set ]";
            else
                shortcutText = shortcut.ToString();
            shortcutEnabled = readBool(key, REGISTRY_KEY_SHORTCUT_ENABLED, false);
            cornerLB = readBool(key, REGISTRY_KEY_CORNER_LB, false);
            cornerRB = readBool(key, REGISTRY_KEY_CORNER_RB, false);
            cornerRT = readBool(key, REGISTRY_KEY_CORNER_RT, false);
            cornerLT = readBool(key, REGISTRY_KEY_CORNER_LT, false);
            showNumbers = readBool(key, REGISTRY_KEY_SHOW_NUMBERS, false);
            showTitles = readBool(key, REGISTRY_KEY_SHOW_TITLES, false);
            showNames = readBool(key, REGISTRY_KEY_SHOW_NAME, false);
            key.Close();
        }

        public void Save()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_KEY_PATH, true);
            if (key == null) key = Registry.CurrentUser.CreateSubKey(REGISTRY_KEY_PATH, RegistryKeyPermissionCheck.ReadWriteSubTree);
            key.SetValue(REGISTRY_KEY_SHORTCUT, (int)shortcut, RegistryValueKind.DWord);
            key.SetValue(REGISTRY_KEY_SHORTCUT_ENABLED, shortcutEnabled.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_CORNER_LB, cornerLB.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_CORNER_RB, cornerRB.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_CORNER_RT, cornerRT.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_CORNER_LT, cornerLT.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_SHOW_NUMBERS, showNumbers.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_SHOW_TITLES, showTitles.ToString(), RegistryValueKind.String);
            key.SetValue(REGISTRY_KEY_SHOW_NAME, showNames.ToString(), RegistryValueKind.String);
            key.Close();
        }

        public static Config Instance()
        {
            if (instance == null)
                instance = new Config();
            return instance;
        }
    }
}
